package com.cn.dream_order_server.zyk.controller;

import com.cn.dream_order_server.async.OrderCartAsync;
import com.cn.dream_order_server.async.UserSiteServerAsync;
import com.cn.dream_order_server.client.GoodServer;
import com.cn.dream_order_server.pojo.UserSite;
import com.cn.dream_order_server.service.OrderOrder1Service;
import com.cn.dream_order_server.util.JsonUtils;
import com.cn.dream_order_server.vo.Cartvo;
import com.cn.dream_order_server.vo.ToCheckoutlVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Controller
public class CheckController {

    @Autowired
    private UserSiteServerAsync userSiteServerAsync;


    @Autowired
    private OrderCartAsync orderCartAsync;

    @Autowired
    private OrderOrder1Service orderOrder1Service;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private GoodServer goodServer;


    @RequestMapping("/toCheckout1")
    public String toCheckout(Map map,String[] title,Long[] price_value,Integer[] shoppingNum,String[] shoppingId){

        //异步执行调用用户服务获取收货地址方法
        Future<List<UserSite>> future = userSiteServerAsync.getUserSiteListByUserId();




        //定义集合用于传到前端
        ArrayList<ToCheckoutlVO> list = new ArrayList<ToCheckoutlVO>();
        //定义变量cartTatol  用于传到前端总金额
        Long cartTatol = 0L;
        //遍历数组存入vo对象中
        for (int i = 0; i <title.length ; i++) {
            ToCheckoutlVO toCheckoutlVO = new ToCheckoutlVO();
            toCheckoutlVO.setShoppingName(title[i]);
            toCheckoutlVO.setShoppingNum(shoppingNum[i]);
            Long shoppingTatol = price_value[i]*shoppingNum[i];
            toCheckoutlVO.setShoppingTatol(shoppingTatol);
            toCheckoutlVO.setShoppingId(shoppingId[i]);
            cartTatol=cartTatol+shoppingTatol;
            System.out.println(toCheckoutlVO);
            list.add(toCheckoutlVO);
        }

        //异步修改数据库信息
        orderCartAsync.wlcBeforeTheSettlementUpdateShoppingCart(list);

        //定义userSiteList用户于传递到前端
        List<UserSite> userSiteList = null;
        try {
            userSiteList = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        //将前端数据添加到map中
        map.put("toCheckoutlVOList",list);
        map.put("cartTatol",cartTatol);
        map.put("userSiteList",userSiteList);

        return "checkout-tatol";
    }


    @PostMapping("/wlcCreateOrder")
    public String wlcCreateOrder(Map map, String[] shoppingId, String userSite_siteId_value, Integer cartTatol, HttpSession session){
        Object user = session.getAttribute("user");
        String orderId = orderOrder1Service.wlccreateOrderOrderSummary(shoppingId, "1", userSite_siteId_value, cartTatol);
        map.put("orderId",orderId);

        return "coming-soon-order";
    }


    @RequestMapping("/wlcCreateOrder_MS_pay")
    public String wlcCreateOrder_MS(Map map, String shoppingId, String userSite_siteId_value, Integer cartTatol, HttpSession session){
        System.out.println(shoppingId+"   "+userSite_siteId_value+"   " +cartTatol);
        Object user = session.getAttribute("user");
        String s = orderOrder1Service.wlccreateOrderOrderSummary_MS(shoppingId, "1", userSite_siteId_value, cartTatol);
        return "redirect:toPayIndex?orderId="+s;
    }


    @GetMapping("/wlcCreateOrder")
    public String wlcCreateOrder(Map map,String orderId){
       /* Object user = session.getAttribute("user");
        String orderId = orderOrderService.wlccreateOrderOrderSummary(shoppingId, "1", userSite_siteId_value, cartTatol);*/
       if (orderId==null||"".equals(orderId)){
           orderId=null;
       }
        stringRedisTemplate.opsForValue().set("create-order-" + orderId,orderId,30,TimeUnit.MINUTES);
        map.put("minute",30);
        map.put("second","00");
        map.put("orderId",orderId);
        return "coming-soon-order";
    }


    @RequestMapping("/wlcGetleftTime")
    @ResponseBody
    public Object wlcGetleftTime(String orderId){
        Long aLong = stringRedisTemplate.getExpire("create-order-" + orderId, TimeUnit.MILLISECONDS);
        return aLong;
    }



    @RequestMapping("/wlcCreateOrder_MS")
    public String wlcCreateOrder_MS(String killed_id,Long commodity_id,Double activity_price,Map map){
        //异步执行调用用户服务获取收货地址方法
        Future<List<UserSite>> future = userSiteServerAsync.getUserSiteListByUserId();

        System.out.println(killed_id+"  "+commodity_id+"    "+activity_price);

        String cartVo = goodServer.getCartVo(commodity_id.toString());
        System.out.println(cartVo);
        Cartvo cartvo = JsonUtils.stringToObj(cartVo, Cartvo.class);
        //创建vo对象
        ToCheckoutlVO toCheckoutlVO = new ToCheckoutlVO();
        toCheckoutlVO.setShoppingId(killed_id);
        double priceTatol = activity_price*100;
        toCheckoutlVO.setShoppingTatol((long)priceTatol);
        toCheckoutlVO.setShoppingNum(1);
        toCheckoutlVO.setShoppingName(cartvo.getSku().getTitle());


        //定义userSiteList用户于传递到前端
        List<UserSite> userSiteList = null;
        try {
            userSiteList = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        //将前端数据添加到map中
        map.put("toCheckoutlVO",toCheckoutlVO);
        map.put("cartTatol",toCheckoutlVO.getShoppingTatol());
        map.put("userSiteList",userSiteList);
        map.put("userSite_siteId_value",userSiteList.get(0));
        return "checkout-tatol-ms";
    }

}
